package com.rainbow.handle

import com.rainbow.emum.ResultEnum
import com.rainbow.entity.Result
import com.rainbow.exception.PersonException
import com.rainbow.util.ResultUtil
import com.rainbow.validate.ValidateCodeException
import lombok.extern.slf4j.Slf4j
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody

/**
 *rainbow
 */
@ControllerAdvice
@Slf4j
class ExceptionHandle {

    @ExceptionHandler(value = [Exception::class])
    @ResponseBody
    fun handle(e: Exception): Result<Any> {
        when (e) {
            is PersonException -> return ResultUtil.error(e.code!!, e.message!!)
            is ValidateCodeException -> return ResultUtil.error(e.code!!, e.message!!)
            is AccessDeniedException -> return ResultUtil.error(ResultEnum.ACCESS_DENIED.code!!, ResultEnum.ACCESS_DENIED.msg!!)
            else -> return ResultUtil.error(-1, "系统未知错误")
        }
    }
}